import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';

@Component({
  selector: 'app-basic',
  templateUrl: './basic.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasicComponent {
  data: string[];
  selectedValue;
  loading = false;

  constructor(private cdr: ChangeDetectorRef) {}

  initData(value, cb) {
    let arr = [];
    if (value.length > 0 && value.length < 5) {
      arr = Array.from({ length: parseInt((Math.random() * 10).toFixed(0), 10) + 5 }).map((item, key) => value + key);
    } else {
      arr = [];
    }

    setTimeout(() => {
      cb(arr);
    }, 500);
  }

  inputChange(e) {
    this.loading = true;
    this.initData(e, data => {
      this.loading = false;
      this.data = data;
      this.cdr.markForCheck();
    });
  }
}
